package com.anglireports.report.data.common.zhujian;

import com.alibaba.fastjson.JSONObject;
import com.anglireports.report.bean.primary.Classroom;
import com.anglireports.report.bean.primary.Course;
import com.anglireports.report.bean.primary.Student;
import com.anglireports.report.bean.primary.Teacher;
import com.anglireports.report.response.result.*;
import com.anglireports.report.response.util.Constant;
import com.anglireports.report.response.util.HttpSendRequest;

import java.util.*;

/**
 * @author lvjilei
 * @ceateTime 2019/3/27.
 */
public class CommonData {

    /**
     * 获取学校和班级下拉框
     *
     * @return
     */
    public static ClassroomsResult getCampusAndCourse(String starttime) {
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("starttime", starttime);
        JSONObject result = HttpSendRequest.doGet(Constant.CLASSROOMANDCOURSE_API, mapParams);
        ClassroomsResult classroomsResult =
                JSONObject.toJavaObject(result, ClassroomsResult.class);
        return classroomsResult;
    }

    /**
     * 获取学校
     *
     * @return
     */
    public static Map<String, Classroom> getClassroomsMap(String starttime) {
        ClassroomsResult classroomsResult = CommonData.getCampusAndCourse(starttime);
        List<Classroom> list = classroomsResult.getClassrooms();
        Map<String,Classroom> classMap = new HashMap<String, Classroom>();
        for (Classroom classroom : list){
            classMap.put(classroom.get_id(), classroom);
        }
        return classMap;
    }

    /**
     * 获取班级
     *
     * @return
     */
    public static Map<String, Course> getCourses(String starttime) {
        ClassroomsResult classroomsResult = CommonData.getCampusAndCourse(starttime);
        List<Classroom> list = classroomsResult.getClassrooms();
        Map<String,Course> courseMap = new HashMap<String, Course>();
        for (Classroom classroom : list){
            List<Course> courses = classroom.getCourses();
            for (Course course : courses){
                course.setStudents(new ArrayList<>());
                course.setClassroom_id(classroom.get_id());
                courseMap.put(course.get_id(), course);
            }
        }
        return courseMap;
    }

    /**
     * 获取班级
     * @return
     */
    public static Map<String, Course> getCourses(Map<String, Classroom> classroomMap) {
        Iterator<String>  iterator = classroomMap.keySet().iterator();
        Map<String,Course> courseMap = new HashMap<String, Course>();
        while (iterator.hasNext()){
            Classroom classroom = classroomMap.get(iterator.next());
            List<Course> courses = classroom.getCourses();
            for (Course course : courses){
                course.setClassroom_id(classroom.get_id());
                courseMap.put(course.get_id(), course);
            }
        }
        return courseMap;
    }

    /**
     * 获取教师
     *
     * @return
     */
    public static TeachersResult getTeachers() {
        JSONObject result = HttpSendRequest.doGet(Constant.TEACHERS_API, null);
        TeachersResult teachersResult =
                JSONObject.toJavaObject(result, TeachersResult.class);
        return teachersResult;
    }

    /**
     * 获取教师
     * @return
     */
    public static Map<String, Teacher> getTeachersMap() {
        TeachersResult teachersResult = CommonData.getTeachers();
        List<Teacher> list = teachersResult.getTeachers();
        Map<String,Teacher> teacherMap = new HashMap<String, Teacher>();
        for (Teacher teacher : list){
            if(teacherMap.get(teacher.get_id()) == null){
                teacherMap.put(teacher.get_id(), teacher);
            }
        }
        return teacherMap;
    }


    /**
     * 获取学生
     *
     * @return
     */
    public static StudentResult getStudents() {
        JSONObject result = HttpSendRequest.doGet(Constant.STUDENT_API, null);
        StudentResult studentResult =
                JSONObject.toJavaObject(result, StudentResult.class);
        return studentResult;
    }

    /**
     * 获取学生Map
     * @return
     */
    public static Map<String, Student> getStudentMap() {
        StudentResult studentResult = CommonData.getStudents();
        List<Student> list = studentResult.getStudents();
        if(list == null || list.size() == 0) {
            return null;
        }

        Map<String,Student> studentMap = new HashMap<String, Student>();
        for (Student student : list){
            if(studentMap.get(student.get_id()) == null){
                studentMap.put(student.get_id(), student);
            }
        }
        return studentMap;
    }

    /**
     * 获取上课记录
     *
     * @return
     */
    public static ReportsResult getReports(String starttime, String endtime,
                                          String course_id, String teacher_id, String student_id) {
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("starttime", starttime);
        mapParams.put("endtime", endtime);
        if (null != course_id){
            mapParams.put("course_id", course_id);
        }
        if (null != teacher_id){
            mapParams.put("teacher_id", teacher_id);
        }
        if (null != student_id){
            mapParams.put("student_id", student_id);
        }
        JSONObject result = HttpSendRequest.doGet(Constant.REPORTS_API, mapParams);
        ReportsResult reportsResult =
                JSONObject.toJavaObject(result, ReportsResult.class);
        return reportsResult;
    }


    /**
     * 获取个人精彩镜头
     *
     * @return
     */
    public static StudentSnaResult getStudentSnapshots(String starttime, String endtime,
                                          String course_id, String student_id) {
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("starttime", starttime);
        mapParams.put("endtime", endtime);

        if (null != course_id){
            mapParams.put("course_id", course_id);
        }

        if (null != student_id){
            mapParams.put("student_id", student_id);
        }

        JSONObject result = HttpSendRequest.doGet(Constant.STUDENTSNAPSHOT_API, mapParams);
        StudentSnaResult studentSnaResult=
                JSONObject.toJavaObject(result, StudentSnaResult.class);
        return studentSnaResult;
    }


    /**
     * 获取集体精彩镜头
     *
     * @return
     */
    public static TeachersSnaResult getTeachersSnapshots(String starttime, String endtime,
                                                   String course_id, String teacher_id) {
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("starttime", starttime);
        mapParams.put("endtime", endtime);
        if (null != course_id){
            mapParams.put("course_id", course_id);
        }
        if (null != teacher_id){
            mapParams.put("teacher_id", teacher_id);
        }
        JSONObject result = HttpSendRequest.doGet(Constant.TEAHCHERSNAPSHOT_API, mapParams);
        TeachersSnaResult teachersSnaResult=
                JSONObject.toJavaObject(result, TeachersSnaResult.class);
        return teachersSnaResult;
    }


}
